package com.nl.spring.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Objects;

/**
 * kpi数据事件发布.
 *
 * @author tianzonghao
 * @date 2021-06-21 11:05
 */
@Slf4j
@Component
public class SpringEventHelper implements ApplicationEventPublisherAware {

    private static ApplicationEventPublisher applicationEventPublisher;


    /**
     * 重写事件发布方法.
     *
     * @param eventPublisher eventPublisher
     */
    @Override
    public void setApplicationEventPublisher(final @NonNull ApplicationEventPublisher eventPublisher) {
        if (Objects.isNull(applicationEventPublisher)) {
            applicationEventPublisher = eventPublisher;
        }
    }

    /**
     * 发布事件.
     *
     * @param event 通知
     */
    public static void publishEvent(final ApplicationEvent event) {
        log.info("发布事件,event:{}", event + " time: " + LocalTime.now());
        SpringEventHelper.applicationEventPublisher.publishEvent(event);
    }

}
